package com.blb.config;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;

public class MyWebApplicationInitializer implements WebApplicationInitializer {

	@Override
	public void onStartup(ServletContext servletContext) {
		//创建IOC容器
		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		//将WebConfig类加入到ioc容器
		context.register(WebConfig.class);

		// 创建DispatcherServlet
		DispatcherServlet servlet = new DispatcherServlet(context);
		// 给DispatcherServlet取个名字
		ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcherServlet", servlet);
		// DispatcherServlet启动的时机
		registration.setLoadOnStartup(1);
		// DispatcherServlet映射路径
		registration.addMapping("/");
	}
}
